package com.fhnew.app.widget;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.os.Vibrator;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.MotionEvent;
import android.view.View;

import com.fhnew.app.util.HLog;

import java.util.ArrayList;
import java.util.List;

public class SelectView extends View {

    private Paint paint;

    private int canvasX = 0;
    private int canvasY = 0;

    private float startTouchX = 0;
    private float startTouchY = 0;

    private List<String> items;

    public SelectView (Context context) {
        super(context);
        init();
    }

    public SelectView (Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public SelectView (Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init(){
        paint = new Paint();
        items = new ArrayList<>();
        for (int i = 0; i < 20; i++) {
            items.add("测试1");
        }
    }

    public void setItems(List<String> items){
        this.items = items;
        invalidate();
    }

    @Override
    protected void onMeasure (int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    @Override
    public boolean onTouchEvent (MotionEvent event) {
        float x =  event.getX();
        float y =  event.getY();
        HLog.I("点击  x="+x +"  y="+y);
        int action = event.getAction();
        switch (action){
            case MotionEvent.ACTION_DOWN:
                startTouchX = x - canvasX;
                startTouchY = y - canvasY;
                break;
            case MotionEvent.ACTION_MOVE:
                canvasX = (int) (x - startTouchX);
                canvasY = (int) (y - startTouchY);
                invalidate();
                break;
            case MotionEvent.ACTION_UP:
                canvasX = (int) (x - startTouchX);
                canvasY = (int) (y - startTouchY);
                break;
        }
        return true;
    }

    @Override
    protected void onDraw (Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawColor(Color.parseColor("#FFFFFF"));
        HLog.I("重新绘制");
        paint.setColor(Color.RED);
        paint.setTextSize(30);
        paint.setStrokeWidth(3);

        canvas.drawLine(0,getHeight()/2-15,getWidth(),getHeight()/2-15,paint);
        canvas.drawLine(0,getHeight()/2+15,getWidth(),getHeight()/2+15,paint);

        drawItem(canvas);
    }

    private void drawItem(Canvas canvas){
        HLog.I("移动 canvasX"+canvasX+"  canvasY"+canvasY);
        int height =  40;

        for (int i = 0; i < items.size() ; i++) {
            canvas.drawRect((getMeasuredWidth()-paint.measureText(items.get(i)))/2,canvasY+height*i-30,(getMeasuredWidth()-paint.measureText(items.get(i)))/2+30,canvasY+height*i,paint);
            canvas.drawText(items.get(i),getMeasuredWidth()/2,canvasY+height*i,paint);
        }
    }

    /**
     * 开始动画
     */
    private void startAnimitor(){

    }

    /**
     * 开始震动
     */
    private  void sensor(){
        Vibrator vibrator = (Vibrator)getContext().getSystemService(Context.VIBRATOR_SERVICE);
        vibrator.vibrate(10);
    }

}
